<?php

namespace App\Models;

use Encore\Admin\Traits\AdminBuilder;
use Illuminate\Database\Eloquent\Model;

/**
 * App\Models\Product
 *
 * @property int $id
 * @property string|null $title
 * @property int|null $price
 * @property int|null $number
 * @property string|null $shareTip
 * @property string|null $shipTip
 * @property string|null $discountTip
 * @property string|null $des
 * @property string|null $notice
 * @property string|null $packageTip
 * @property string|null $shouTip
 * @property string|null $info
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Product whereDes($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Product whereDiscountTip($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Product whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Product whereInfo($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Product whereNotice($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Product whereNumber($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Product wherePackageTip($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Product wherePrice($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Product whereShareTip($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Product whereShipTip($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Product whereShouTip($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Product whereTitle($value)
 * @mixin \Eloquent
 */
class Product extends Model
{
    protected $fillable = ['id','updated_at','created_at'];

    static $install_price = 150;
    static $open_acount_price = 300;
    static $service_price = 980;
    static $discount_price = 200;
    static $commission_price = 200;
    static $default_image = 'http://upload.cheaa.com/2016/0504/1462325326710.png';
    use AdminBuilder;
    public static function formatInfo($info){
        $lines = explode("\r\n", $info);
        $cond = [];
        if (count($lines) > 0) {
            foreach ($lines as $line) {
                $item = explode(' ',  $line);
                if ($item) {
                    $i['title'] = isset($item[0])?$item[0]:'';
                    $i['value'] = isset($item[1])?$item[1]:'';
                    $cond[] = $i;
                }
            }
        }
        return $cond;
    }
    public function images(){
       return  $this->hasMany(ProductImage::class)->orderBy('sort','asc');
    }
    public function setImageAttribute($pictures)
    {
        if (is_array($pictures)) {
            $this->attributes['image'] = json_encode($pictures);
        }
    }

    public function getImageAttribute($pictures)
    {
        return json_decode($pictures, true);
    }
    public function getKey()
    {
        return parent::getKey(); // TODO: Change the autogenerated stub
    }

    public function getTable()
    {
        return parent::getTable(); // TODO: Change the autogenerated stub
    }
}
